<!--- Constant Variables --->
<CFSET CONDITION_PRIORITYNO=1>
<CFSET CONDITION_ALIASSOURCE=2>
<CFSET CONDITION_SOURCEFIELD=3>
<CFSET CONDITION_OPERATOR=4>
<CFSET CONDITION_VALUEORALIAS=5>
<CFSET CONDITION_ALIASJOINING=6>
<CFSET CONDITION_JOININGOPERATOR=7>
<CFSET CONDITION_GROUPSTART=8>
<CFSET CONDITION_GROUPEND=9>
<CFSET sDelimiter="~">
<CFSET sUpdateMsg="">


<CFPARAM NAME="form.condition" DEFAULT="">
<CFSET listCondition=#form.condition#>

<CFTRANSACTION>
<CFTRY>
	<CFLOOP INDEX="i" LIST="#form.chkpid#">
	<!--- save UserFilter Details--->
		<CFIF i NEQ "">
			<CFQUERY NAME="isExist" DATASOURCE=#client.company_dsn#>
				SELECT * FROM ECRGUSERFILTER
				WHERE FORMCODE='powerWSUser'
					AND USERFILTERID='#i#'
			</CFQUERY>
			<CFIF isExist.RECORDCOUNT>
				<CFQUERY NAME="qryUpdateECRGDETAILS" DATASOURCE=#client.company_dsn#>
					UPDATE ECRGUSERFILTER
					SET DESCRIPTION = '#form.txtDescription#'
					WHERE USERFILTERID = '#i#'
				</CFQUERY>
				<CFQUERY NAME="qryECRGUSERFILTERCONDITION" DATASOURCE=#client.company_dsn#>
					DELETE FROM ECRGUSERFILTERCONDITION
					WHERE (USERFILTERID = '#i#')
				</CFQUERY>
			<CFELSE>
				<CFQUERY NAME="qryECRGDETAILS" DATASOURCE=#client.company_dsn#>
					INSERT INTO ECRGUSERFILTER
					(FORMCODE, USERFILTERID, DESCRIPTION, ISDEFAULT,USERFILTERTYPE)
					VALUES ('powerWSUser', '#i#', '#form.txtDescription#', 'Y', 'USERDEFINED')
				</CFQUERY>
			</CFIF>
	
			<CFLOOP LIST="#listCondition#" INDEX="sValue">
				<CFSET sPriorityNo=Right("0" & ListGetAt(#sValue#, CONDITION_PRIORITYNO, #sDelimiter#), 2)>
				<CFSET sAliasSource=ListGetAt(#sValue#, CONDITION_ALIASSOURCE, #sDelimiter#)>
				<CFSET sSourceField=ListGetAt(#sValue#, CONDITION_SOURCEFIELD, #sDelimiter#)>
				<CFSET sOperator=ListGetAt(#sValue#, CONDITION_OPERATOR, #sDelimiter#)>
				<CFSET sValueOrAlias=Replace(ListGetAt(#sValue#, CONDITION_VALUEORALIAS, #sDelimiter#), "'", "''", "ALL")>
				<CFSET sValueOrAlias=PreserveSingleQuotes(sValueOrAlias)>
				<CFSET sAliasJoining=ListGetAt(#sValue#, CONDITION_ALIASJOINING, #sDelimiter#)>
				<CFSET sJoiningOperator=ListGetAt(#sValue#, CONDITION_JOININGOPERATOR, #sDelimiter#)>
				<CFSET sGroupStart=ListGetAt(#sValue#, CONDITION_GROUPSTART, #sDelimiter#)>
				<CFSET sGroupEnd=ListGetAt(#sValue#, CONDITION_GROUPEND, #sDelimiter#)>
				
	
				<CFQUERY NAME="qryECRGUSERFILTERCONDITION" DATASOURCE=#client.company_dsn#>
					INSERT INTO ECRGUSERFILTERCONDITION
						(USERFILTERID,
						PRIORITYNO,
						ALIAS,
						SOURCEFIELD,
						OPERATOR,
						VALUEORALIAS,
						JOININGFIELD,
						JOININGOPERATOR,
						GROUPSTART,
						GROUPEND)
					VALUES
						('#i#',
						'#sPriorityNo#',
						'#sAliasSource#',
						'#sSourceField#',
						'#sOperator#',
						'#sValueOrAlias#',
						'#sAliasJoining#',
						'#sJoiningOperator#',
						'#sGroupStart#',
						'#sGroupEnd#')
				</CFQUERY>
			</CFLOOP>
		</CFIF>
	</CFLOOP>
	<CFTRANSACTION ACTION="COMMIT"/>
	<CFSET sMess="User Filter Successfully Saved. ">
	<CFCATCH TYPE="Database">
		<CFTRANSACTION ACTION="ROLLBACK"/>
		<CFSET sWarn="Error in saving user filter ...<br><br>#CFCATCH.message# #CFCATCH.DETAIL#">
	</CFCATCH>
</CFTRY>
</CFTRANSACTION>
